import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class VariablePanel(QWidget):
    picPath= "./com/mat/rpa/views/workWindow/images/"
    def __init__(self, parent=None):
        super(VariablePanel, self).__init__(parent)
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0,0,0,0)
        self.btnBar = QWidget()
        self.btnBar.setObjectName("btnBar")
        self.btnBarLayout = QHBoxLayout()
        self.btnBarLayout.setContentsMargins(0,0,0,0)
        self.addBtn = QPushButton()
        self.addBtn.setIcon(QIcon(QPixmap("./view/middlePanel/images/add.png")))
        self.btnBarLayout.addWidget(self.addBtn)
        self.minusBtn = QPushButton()
        self.minusBtn.setIcon(QIcon(QPixmap("./view/middlePanel/images/minus.png")))
        self.btnBarLayout.addWidget(self.minusBtn)
        self.btnBarLayout.addStretch(0)
        self.btnBar.setLayout(self.btnBarLayout)
        self.addBtn.clicked.connect(self.addRow)
        self.minusBtn.clicked.connect(self.removeRows)
        layout.addWidget(self.btnBar)
        self.variableTable = QTableWidget()
        self.variableTable.setColumnCount(4)
        self.variableTable.setHorizontalHeaderLabels(["名称", "变量类型", "范围", "默认值"])
        self.variableTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        layout.addWidget(self.variableTable)
        layout.setSpacing(0)

    def createVariableCombo(self):
        combobox = QComboBox()
        combobox.addItem("bool")
        combobox.addItem("int32")
        combobox.addItem("float64")
        combobox.addItem("str")
        combobox.addItem("list")
        combobox.addItem(("tuple"))
        combobox.addItem("dict")
        combobox.addItem("set")
        combobox.setStyleSheet("QComboBox {margin:3px};")
        return combobox

    def createScopeCombo(self):
        combobox = QComboBox()
        combobox.addItem("Sequence")
        combobox.addItem("Flow Chart")
        combobox.addItem("State Machine")
        # combobox.setStyleSheet("QComboBox {margin:3px; font-size: 26px};")
        return combobox

    def addRow(self):
        row = self.variableTable.rowCount()
        self.variableTable.insertRow(row)
        check = QTableWidgetItem()
        check.setCheckState(Qt.Unchecked)
        self.variableTable.setItem(row, 0, check)
        variableCombobox = self.createVariableCombo()
        self.variableTable.setCellWidget(row,1,variableCombobox)
        scopeCombobox = self.createScopeCombo()
        self.variableTable.setCellWidget(row,2,scopeCombobox)

    def removeRows(self):
        i = 0 #初始从第0行开始
        while i < self.variableTable.rowCount():#每次判断都会重新计算表格行数
            checked = self.variableTable.item(i,0).checkState() #获得当前行checkbox的勾选状态
            if checked:#假如当前行被选中，则删除行号为i的行
                self.variableTable.removeRow(i)
            else:#如果当前行没有被选中，则行计数i+1
                i += 1


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = VariablePanel()
    win.show()
    sys.exit(app.exec_())